using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RSSApplicationUpdater
{
    public partial class WebBrowser : Form
    {
        bool addfeed;
        RssItem article_rss;

        public WebBrowser()
        {
            InitializeComponent();
            this.hidebuttonsenabledefaultmenusandShowDownlaodbutton();
        }
        /// <summary>
        /// if addfeed is true this enables the 
        /// ability to edit the update feed
        /// </summary>
        /// <param name="addfeed">true toenables the 
        /// ability to edit the update feed</param>
        public WebBrowser(bool addfeed)
        {
            this.AddFeed = addfeed;
            InitializeComponent();
            this.hidebuttonsenabledefaultmenusandShowDownlaodbutton();

            
        }
        /// <summary>
        /// This disable the ability to hange the update feed
        /// and visit's the given url.
        /// </summary>
        /// <param name="article_rss">the rss feed that  the form will visit
        /// </param>
        public WebBrowser(RssItem  article_rss)
        {

           if (article_rss != null)
            {
                AddFeed = false;


                InitializeComponent();
                this.hidebuttonsenabledefaultmenusandShowDownlaodbutton();
                this.Article_RSS  = article_rss;
                if (article_rss != null)
                {
                    //MessageBox.Show(article_rss.Link);
                    // webBrowser1.DocumentText = article_rss.Description;
                    //MessageBox.Show(article_rss.Link);
                    webBrowser1.Navigate(article_rss.Link);
                }
               
                
              
            }
        }

        /// <summary>
        /// Sets or Gets the rss item of the article
        /// </summary>
        public RssItem Article_RSS
        {
            get { return this.article_rss; }
            set { article_rss = value; }


        }
        void hidebuttonsenabledefaultmenusandShowDownlaodbutton()
        {
            try
            {
                if (addfeed != true)
                {
                    
                    this.btnCancel.Hide();
                    this.btnOk.Hide();
                    this.btnDownloadUpddate.Show();
                    webBrowser1.Show();
                    webBrowser1.IsWebBrowserContextMenuEnabled = true;
                    webBrowser1.ContextMenu = null;
                    mnuChangeFeed.Dispose();
                    this.cmbReleaseState.Hide();
                    this.label1.Hide();




                }


            }
            catch (Exception ex)
            {
                Core.errorreport(ex);
                
            }
        }
        public bool AddFeed
        {
            get { return addfeed ; }
            set {addfeed = value; }

        }
        ApplicationUpdater appupd = new ApplicationUpdater(true);
        Properties props;

        private void btnCancel_Click(object sender, EventArgs ev)
        {
            try
            {
                this.Close();
                this.Dispose();


            }
            catch (Exception e)
            {

                Core.errorreport(e);
            }
        }

        private void WebBrowser_Load(object sender, EventArgs e)
        {
            try
            {
                appupd.GetConfig();
                props = appupd.properties;
                if (addfeed == true)
                {
                    if ((props != null) || (props.Application_WebSite != null) )
                    {

                        this.webBrowser1.Navigate(props.Application_WebSite);
                        this.cmbReleaseState.Text = props.Release_State;





                    }
                }
                else 
                {
                   
                    if (article_rss != null)
                        {
                            //MessageBox.Show(article_rss.Link);
                           // webBrowser1.DocumentText = article_rss.Description;
                            //MessageBox.Show(article_rss.Link);
                            webBrowser1.Navigate(article_rss.Link);
                        }
                }
                


            }
            catch (Exception ex)
            {

                Core.errorreport(ex);
            }

        }

        private void addFeedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                HtmlElement  elemn = webBrowser1.Document.ActiveElement;
                if ((props != null) ||(elemn !=null)|| (elemn.TagName   == "a"))
                {

                   // MessageBox.Show(elemn.GetAttribute("href"));
                   props.URI = elemn.GetAttribute("href");
                 
                   

                }


            }
            catch (Exception ex)
            {

                Core.errorreport(ex);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if(this.cmbReleaseState.Text !=null)
            {
                props.Release_State= cmbReleaseState.Text;
                appupd.properties = props;
                appupd.SaveConfig();
                this.Close();
            }


            }
            catch (Exception ex)
            {

                Core.errorreport(ex);
            }

        }

        private void btnCancel_Click_1(object sender, EventArgs e)
        {

        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            try
            {

                this.Text = webBrowser1.DocumentTitle;
            }
            catch (Exception ex)
            {
                Core.errorreport(ex);

            }

        }

        private void btnDownloadUpddate_Click(object sender, EventArgs e)
        {
            try
            {

                if (article_rss != null)
                {

                    appupd.DownloadNewVersionFromFeed(article_rss.Enclosure_url);
                }
            }
            catch (Exception ex)
            {
                Core.errorreport(ex);

            }

        }

       
    }
}